package tw.fc.agui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class GraphicPanel extends GrPanel {

   //[ 繪圖物件管理
   /**
    * 繪圖物件管理容器
    */
   protected GrObjManager GrObjM=null;        
   
   //[ ---------- construct ----------
   public GraphicPanel(Context context) {
      super(context);
   }
   public GraphicPanel(Context context, int w, int h) {
      super(context, w, h);
   }
   public GraphicPanel(Context context, AttributeSet attrs, int w, int h) {
      super(context, attrs, w, h);
   }
   public GraphicPanel(Context context, AttributeSet attrs, int defStyle,
         int w, int h) {
      super(context, attrs, defStyle, w, h);
   }
   //] ---------- construct ----------
   
   /**
    * 取得畫面上繪圖物件數量.
    */
   public int getObjCount() {  return GrObjM.size(); }
   /**
    * 將繪圖物件加到 GrObjM中.
    * @param ob 自定義的繪圖物件.
    */
   public void addObj(GraphicObj ob) {
      if(GrObjM == null) GrObjM = new GraphicObjManager(this);
      GrObjM.add(ob);        //: last-most
      ob.setPanel(this);     //: 子類繪圖物件由ob自行處理
   }
   /**
    * 將繪圖物件自 GrObjM中移除.
    * @param ob 指定的繪圖物件.
    */
   public void removeObj(GraphicObj ob) {
      if(GrObjM != null){
         GrObjM.remove(ob);
         ob.setPanel(null);   //: 子類繪圖物件由ob自行處理
      }
   }
   /**
    * 自 GrObjM取出繪圖物件.
    * @param idx 物件編號.
    * @return 與編號對應的物件.
    */
   public GraphicObj getObj(int idx) {
      if(GrObjM != null){
         return (GraphicObj)GrObjM.get(idx);
      }
      else
         return null;
   }
   /**
    * 指定物件在 GrObjM內的編號, 若不存在將回傳 -1.
    * @param ob 指定的繪圖物件.
    * @return 它在 GrObjM內的編號.
    */
   public final int indexOf(GraphicObj ob) {  //: 不屬於則-1
      if(GrObjM != null){
         return GrObjM.indexOf(ob);
      }
      else
         return -1;
   }
   /**
    * 根據你指定的位置, 判斷 GrObjM內是否有繪圖物件被點中, 若無則回傳 null.   
    * @param at 指定的位置.
    * @return 被點中的物件.
    */
   public GraphicObj findObjAt(ImuV2D at) {
      if(GrObjM != null){
         return (GraphicObj)GrObjM.findObjAt(at);
      }
      else
         return null;
   }
   /**
    * 使指定的物件在重繪時顯示在最上層.
    * @param ob 指定的繪圖物件.
    */
   public void toTop(GraphicObj ob) {
      if(GrObjM != null){
         GrObjM.toTop(ob);
      }
   }
   /**
    * 繪製 GrObjM內所有的物件.
    * @param g GrCanvas.
    */
   public void paintAllObj(GrCanvas g) {
      if(GrObjM != null){
         ((GraphicObjManager)GrObjM).paintAllObj(g);
      }
   }
   
   //[ **
   //[ 將事件轉發配到 GrObjM對應的處理函數
   //[ GrObjM會在轉發配一次給當前操作的繪圖物件
   //[ 繪圖物件針對各種事件, 去做反應.
   @Override 
   public boolean onFling(MotionEvent e1, MotionEvent e2, 
                          float velocityX, float velocityY){ 
      if(GrObjM != null){
         GrObjM.onFling(e1, e2, velocityX, velocityY);
      }
      return true;
   }
   @Override 
   public boolean onScroll(MotionEvent e1, MotionEvent e2, 
                           float distanceX, float distanceY){
      if(GrObjM != null){
         GrObjM.onScroll(e1, e2, distanceX, distanceY);
      }
      return true;
   }
   @Override 
   public boolean onDown(MotionEvent e) { 
      if(GrObjM != null){
         GrObjM.onDown(e);
      }
      return true; 
   }
   @Override 
   public boolean onDoubleTap(MotionEvent e) { 
      if(GrObjM != null){
         GrObjM.onDoubleTap(e);
      }
      return true;
   }
   @Override 
   public boolean onDoubleTapEvent(MotionEvent e) { 
      if(GrObjM != null){
         GrObjM.onDoubleTap(e);
      }
      return true;
   }
   @Override 
   public boolean onSingleTapConfirmed(MotionEvent e) { 
      if(GrObjM != null){
         GrObjM.onSingleTapConfirmed(e);
      }
      return true; 
   }
   @Override 
   public boolean onSingleTapUp(MotionEvent e) { 
      if(GrObjM != null){
         GrObjM.onSingleTapUp(e);
      }
      return true; 
   }
   @Override 
   public void onLongPress(MotionEvent e) {
      if(GrObjM != null){
         GrObjM.onLongPress(e);
      }
   }
   @Override 
   public void onShowPress(MotionEvent e) {
      if(GrObjM != null){
         GrObjM.onShowPress(e);
      }
   }
   @Override 
   public boolean onMove(MotionEvent e) { 
      if(GrObjM != null){
         GrObjM.onMove(e);
      }
      return true; 
   }
   @Override 
   public boolean    onRelease(MotionEvent e) { 
      if(GrObjM != null){
         GrObjM.onRelease(e);
      }
      return true; 
   }
   @Override 
   public boolean onMultiTouchDown(MotionEvent e) { 
      if(GrObjM != null){
         GrObjM.onMultiTouchDown(e);
      }
      return true; 
   }
   @Override 
   public boolean onMultiTouchUp(MotionEvent e) { 
      if(GrObjM != null){
         GrObjM.onMultiTouchUp(e);
      }
      return true; 
   }
   //]
}
